define(['jquery', 'bootstrap', 'frontend', 'template', 'form','echarts','table' , 'bootstrap-datetimepicker','backend/csmonline/chart/_chartregisterdate'],
function ($, undefined, Frontend, Template, Form,echarts,Table,datetimepicker,mychart) {
	Date.prototype.format = function (fmt) {
	    var o = {
	        "M+": this.getMonth() + 1, //月份
	        "d+": this.getDate(), //日
	        "h+": this.getHours(), //小时
	        "m+": this.getMinutes(), //分
	        "s+": this.getSeconds(), //秒
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
	        "S": this.getMilliseconds() //毫秒
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	};
	
    var Controller = {
        index: function () {
            // 初始化表格参数配置
        	var that = this;
        	that._dateselect();
        	mychart.init(that,'chart1');
            Table.api.init({
                extend: {
                    index_url: 'csmonline/chartregisterdate/index'
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'statdate', title: __('日期'), operate: '=','colspan':1},
                        
                        {field: 'allregisteredus', title: __('总注册数'),'colspan':1},                        
                        {field: 'newregisteredus', title: __('新增注册'),'colspan':1},                         
                        {field: 'dau', title: __('日活'),'colspan':1
                            , formatter: function (value, row, index) {
                                return value+"%";
                            }},                         
                    ]
                ],
                //普通搜索
                commonSearch: false,
                titleForm: '', //为空则不显示标题，不定义默认显示：普通搜索,
                queryParams: function (params) {
                	console.log(params);
                	console.log(params.filter);
//                	params.filter = '{"envtype":"calismobile"}';

                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    
                    filter.envtype = 'calismobile';
                    op.envtype = '=';
                    
//                  var day1 = new Date();
//                  var day2 = new Date();
//                  day1.setDate(day1.getDate() - 1);
//                  day2.setDate(day2.getDate() - 1);
//                  var s1 = day1.format("yyyy-MM-dd");
//                  var s2 = day2.format("yyyy-MM-dd");
//                  $("#datetimepicker1_input").val(s1);
//                  $("#datetimepicker2_input").val(s2);
                    var s1 = $("#datetimepicker1_input").val();
                    var s2 = $("#datetimepicker2_input").val();
                    
                    filter.createtime = s1+" 00:00:00 - "+s2+" 23:59:59";
                    op.createtime = 'RANGE';
                    
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    
                    params.begin = s1;
                    params.end = s2;
                    
                    mychart._getDataAndDraw();
                    
                    return params;
                },
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    var params = queryParams(params);
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    if (value !== '') {
                        filter[field] = value;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };

                table.bootstrapTable('refresh', {});
                mychart._getDataAndDraw();
                return false;
            });
            
            // 指定搜索条件
            $(document).on("click", "#before1", function () {
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    var day1 = new Date();
                    var day2 = new Date();
                    day1.setDate(day1.getDate() - 1);
                    day2.setDate(day2.getDate() - 1);
                    var s1 = day1.format("yyyy-MM-dd");
                    var s2 = day2.format("yyyy-MM-dd");
                    $("#datetimepicker1_input").val(s1);
                    $("#datetimepicker2_input").val(s2);
                    
                    filter.createtime = s1+" 00:00:00 - "+s2+" 23:59:59";
                    op.createtime = 'RANGE';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    params.begin = s1;
                    params.end = s2;
                    
                    that._renderbeforebtn("#before1");
                    return params;
                };
                table.bootstrapTable('refresh', {});
                mychart._getDataAndDraw();
                return false;
            });   
            $(document).on("click", "#before7", function () {
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    var day1 = new Date();
                    var day2 = new Date();
                    day1.setDate(day1.getDate() - 7);
                    day2.setDate(day2.getDate() - 1);
                    var s1 = day1.format("yyyy-MM-dd");
                    var s2 = day2.format("yyyy-MM-dd");
                    $("#datetimepicker1_input").val(s1);
                    $("#datetimepicker2_input").val(s2);
                    
                    filter.createtime = s1+" 00:00:00 - "+s2+" 23:59:59";
                    op.createtime = 'RANGE';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    params.begin = s1;
                    params.end = s2;
                    
                    that._renderbeforebtn("#before7");
                    return params;
                };
                table.bootstrapTable('refresh', {});
                mychart._getDataAndDraw();
                return false;
            });  
            $(document).on("click", "#before30", function () {	
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    var day1 = new Date();
                    var day2 = new Date();
                    day1.setDate(day1.getDate() - 30);
                    day2.setDate(day2.getDate() - 1);
                    var s1 = day1.format("yyyy-MM-dd");
                    var s2 = day2.format("yyyy-MM-dd");
                    $("#datetimepicker1_input").val(s1);
                    $("#datetimepicker2_input").val(s2);
                    
                    filter.createtime = s1+" 00:00:00 - "+s2+" 23:59:59";
                    op.createtime = 'RANGE';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    params.begin = s1;
                    params.end = s2;
                    
                    that._renderbeforebtn("#before30");
                    
                    return params;
                };
                table.bootstrapTable('refresh', {});
                mychart._getDataAndDraw();
                return false;
            }); 
            $(document).on("click", "#dateconfirm", function () {	
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};

                    var s1 = $("#datetimepicker1_input").val();
                    var s2 = $("#datetimepicker2_input").val();
                    
                    filter.createtime = s1+" 00:00:00 - "+s2+" 23:59:59";
                    op.createtime = 'RANGE';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    params.begin = s1;
                    params.end = s2;
                    
                    that._renderbeforebtn("#dateconfirm");
                    
                    return params;
                };
                table.bootstrapTable('refresh', {});
                mychart._getDataAndDraw();
                return false;
            });  
        },
        _renderbeforebtn:function(activebtn){
            $("#before1").removeClass("btn-info");
            $("#before1").addClass("btn-default");
            $("#before7").removeClass("btn-info");
            $("#before7").addClass("btn-default");
            $("#before30").removeClass("btn-info");  
            $("#before30").addClass("btn-default");
            $("#dateconfirm").removeClass("btn-info");  
            $("#dateconfirm").addClass("btn-default");
            $(activebtn).addClass("btn-info");
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        _dateselect:function(){
        	var that = this;
			var day1 = new Date();
			var s1 = day1.format("yyyy-MM-dd");
			var day2 = new Date();
			day2.setDate(day1.getDate() - 7);
			var s2 = day2.format("yyyy-MM-dd");
            that._core_bui_datetimepicker('#datetimepicker1',s2,function(val){
            });
            that._core_bui_datetimepicker('#datetimepicker2',s1,function(){
            });   
            
        },
        _core_bui_datetimepicker:function(jquerydomkey,date,onchangeFunc){
		    $(jquerydomkey).datetimepicker({
		        showClose:true,
		        format:'YYYY-MM-DD',
		    }).on('dp.change',function (ev) {
		        var v = $(jquerydomkey).val();
		        onchangeFunc(v);
		    });
		    $(jquerydomkey+"_input").val(date);
		}  
    };
    return Controller;
});